// ==UserScript==
// @name         Tenpay
// @namespace    http://www.baidu.com/
// @version      0.12.3
// @description  G63_Tx
// @author       Tenpay
// @match        *://pay.qq.com/h5/index.shtml*
// @include      *://pay.qq.com/h5/shop.shtml?*
// @include      *://pay.qq.com/h5/shop.shtml#*
// @include      *://pay.qq.com/h5/shop.shtml*
// @match        *://pay.qq.com/midas/minipay_v2/views/cpay/game.shtml*
// @match        *://pay.qq.com/paygame/index.shtml*
// @match        *://xinyue.qq.com/*
// @match        *://act.xinyue.qq.com/*
// @match        *://pay.qq.com/midas/minipay_v2/views/cpay/goods.shtml*
// @connect      *
// @license      MIT
// @grant        GM_info
// @grant        unsafeWindow
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_getResourceText
// @grant        GM_xmlhttpRequest
// @grant        GM_setClipboard
// @grant        GM_getResourceURL
// @require      https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.6.0/jquery.min.js
// @require      https://cdn.jsdelivr.net/npm/js-base64@3.7.5/base64.js
// @require      https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/qrcodejs/1.0.0/qrcode.min.js
// @resource     CxSecretsFont https://static.muketool.com/scripts/cx/v2/fonts/cxsecret.json
// @resource     Layui https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/layui/2.6.8/css/layui.min.css
// @resource     LayuiIconFont-woff2 https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/layui/2.6.8/font/iconfont.woff2
// @resource     LayuiIconFont-woff https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/layui/2.6.8/font/iconfont.woff
// @resource     LayuiIconFont-ttf https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/layui/2.6.8/font/iconfont.ttf
// ==/UserScript==

var _self = unsafeWindow,
  url = location.pathname,
  top = _self;

(function() {
  if(document.referrer===''){
    window.open(location.href)
  }
})();


GM_addStyle(`
@import url(${GM_getResourceURL('Layui')});
`);

GM_addStyle(`
@font-face {
font-family: 'layui-icon';
src: url(${GM_getResourceURL('LayuiIconFont-woff2')}) format('woff2'),
 url(${GM_getResourceURL('LayuiIconFont-woff')}.woff) format('woff'),
 url(${GM_getResourceURL('LayuiIconFont-ttf')}) format('truetype');
font-weight: normal;
font-style: normal;
}
`);

var $ = _self.jQuery || top.jQuery,
  parent = _self == top ? self : _self.parent,
  Ext = _self.Ext || parent.Ext || {},
  UE = _self.UE,
  Base64 = self.Base64,
  vjs = _self.videojs;








// 创建 panel 的 HTML 结构
var panelHTML = `
        <style>
            .qr-code-txt {
                text-align: center;
                margin-bottom: 7px;
                font-family: 'helvetica neue', 'hiragino sans gb', PingFangSC-Light, Arial, tahoma, 'microsoft yahei ui', 'microsoft yahei', '微软雅黑', simsun, sans-serif;
                font-size: 14px;
            }

            .qr-code-area .qr-code-container {
                position: relative;
                width: 140px;
                height: 140px;
                display: inline-block;
                padding: 9px;
                border: 1px solid #e5e5e5;
            }
            .qr-code-area{
                text-align: center;
            }
            .qr-code-area .qr-code-container.qq {
                border-bottom-color: #3387ef;
            }

            .qr-code-txt .icon-qq,
            .qr-code-txt .icon-wx {
                margin-right: 4px;
            }
            em {font-style: normal;}

            .icon-qq {
                background-image: url(https://midas.gtimg.cn/midas/minipay_v2/images/sprite@2x.c6e1a4965de7a4069e10e6f1bb463a8c.png);
                background-position: -69px -81px;
                background-size: 226px 210px;
                width: 14px;
                height: 17px;
            }

            .qr-code-txt em {
                font-size: 20px;
                font-weight: 400;
                margin: 0 2px;
            }

            #container {
                margin: 0 auto;
            }
        </style>
        <div>
        <input type="text" id="dynamicInput" value="" style="position: fixed; top: 30%; z-index: 999999; border: 1px red solid; height: 40px;">
        </div>
        <div id="panel" style="display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: white; border: 1px solid black; padding: 20px;">
            <button id="showHideButton">隐藏</button>
            <button id="renewalButton">更换</button>
            <div id="container" style="margin-top:10px">
                <div class="qr-code-txt">
                    <span class="icon-qq">&nbsp;&nbsp;&nbsp;&nbsp;</span>
                    手Q扫码，支付
                    <em id="editableNumber">5</em>
                    元
                </div>
                <div class="qr-code-area">
                    <div class="qr-code-container qq">
                        <div id="qrcode" style="width:140px; height:140px; margin-top:0px;" title="https://myun.tenpay.com/mqq/pay/index.shtml?_wv=1027&amp;app_jump=1&amp;t=">
                            <img id="qrImg" width="140px" src="" style="display: block;">
                        </div>
                    </div>
                    <div class="qr-code-container qq" style="display: none;"></div>
                </div>
            </div>
        </div>
    `;

// 在页面中插入 panel
document.body.insertAdjacentHTML('beforeend', panelHTML);

// 获取 panel 和按钮元素
var panel = document.getElementById('panel');
var showHideButton = document.getElementById('showHideButton');
var renewalButton= document.getElementById('renewalButton');
showHideButton.style.color='red';
panel.style.zIndex = 99999;
panel.style.width = '300px';
// 添加按钮点击事件
showHideButton.addEventListener('click', function() {
  if (panel.style.display === 'none') {
    panel.style.display = 'block';
    showHideButton.textContent = '关闭';
  } else {
    panel.style.display = 'none';
    showHideButton.textContent = '显示';
  }
});

renewalButton.addEventListener('click', function() {
  // 更换二维码
  var rqimg = document.getElementById('qrImg');
  rqimg.style.display='none';
  var qrcode = new QRCode(document.getElementById("qrcode"), {
    width : 140,
    height : 140,
    correctLevel : QRCode.CorrectLevel.L
  });
  function makeCode () {
    var tokenId = GM_getValue('tokenId');
    var value=`https://myun.tenpay.com/mqq/pay/index.shtml?_wv=1027&app_jump=1&t=${tokenId}`
    qrcode.makeCode(value);
  }
  makeCode();
});




//更换二维码内容
function changeQrCode(token,count) {
  // 获取二维码图片元素
  var rqimg = document.getElementById('qrImg');
  rqimg.src=`https://pay.qq.com/cgi-bin/account/get_qr_image.cgi?size=120&url=https%3A%2F%2Fmyun.tenpay.com%2Fmqq%2Fpay%2Findex.shtml%3F_wv%3D1027%26app_jump%3D1%26t%3D${token}`

  //获取数量元素
  var  editableNumber = document.getElementById('editableNumber');
  editableNumber.textContent = count
  panel.style.display = 'block';
}





function addXMLRequestCallback(callback){
  var oldSend, i;
  if( XMLHttpRequest.callbacks ) {
    XMLHttpRequest.callbacks.push( callback );
  } else {
    XMLHttpRequest.callbacks = [callback];
    oldSend = XMLHttpRequest.prototype.send;
    XMLHttpRequest.prototype.send = function(){
      for( i = 0; i < XMLHttpRequest.callbacks.length; i++ ) {
        XMLHttpRequest.callbacks[i]( this );
      }
      // console.log('this---',this)
      if(this && this.W &&  this.W.url || this && this.aegisUrl){
        var geturl = "";
        if(this.aegisUrl){
          geturl = this.aegisUrl;
        }else{
          geturl = this.W.url;
        }
        //console.log('geturl---',geturl)
        //  console.log('mobile_save---',geturl.indexOf("/mobile_save"))
        if(geturl  && geturl.indexOf("/web_save")!=-1 || geturl && geturl.indexOf("/mobile_save")!=-1){
          var getdata =arguments[0];
          getdata=unescape(getdata);
          // console.log('arguments[0]---->', getdata);
          var str= getdata.match(new RegExp(`buy_quantity=(.*?)&`))[1];
          var str1= getdata.match(new RegExp(`&amt=(.*?)&`))[1];
          GM_setValue("buy_quantity", str);
          GM_setValue("adddata", getdata);
          GM_setValue("amt", str1);
          console.log('amt=是---->', GM_getValue("amt"),str1);
          console.log('buy_quantity=是---->', GM_getValue("buy_quantity"),str);
        }
      }
      oldSend.apply(this, arguments);
    }
  }
}
addXMLRequestCallback( function( xhr ) {
  xhr.addEventListener("load", function(){
    if ( xhr.readyState == 4 && xhr.status == 200 ) {
      //https://api.unipay.qq.com/v1/r/1450013297/web_save
      if ( xhr.responseURL.search("(.*?)/v1/(.*?)/(.*?)/mobile_save") != -1 || xhr.responseURL.search("(.*?)/v1/r/(.*?)/web_save") != -1) {
        //console.log(Base64.encode(xhr.response));
        // alert(xhr.response);
        var resp = JSON.parse(xhr.response);
        console.log('resp',resp)
        if(resp.ret == 0){
          // alert(resp.qqwallet_info.qqwallet_tokenId)
          // console.log('resp',resp)
          // panel.style.display = 'block';
          changeQrCode(resp.qqwallet_info.qqwallet_tokenId,GM_getValue("buy_quantity")/100)
          GM_setValue("tokenId", resp.qqwallet_info.qqwallet_tokenId);
        }
      }
    }
  });
});
